<template>
  <div class="app-container">
    <!-- 添加或修改OA 请假申请对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" v-if="open" append-to-body>
      <el-form ref="leaveApproveRef" :model="form" :rules="rules" label-width="110px" :disabled="disabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择请假类型">
                <el-option
                    v-for="dict in leave_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="请假原因" prop="reason">
              <el-input v-model="form.reason" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker
                  clearable size="small"
                  v-model="form.startTime"
                  type="datetime"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  placeholder="选择开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker
                  clearable size="small"
                  v-model="form.endTime"
                  type="datetime"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  placeholder="选择结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假天数" prop="day">
              <el-input-number
                  v-model="form.day"
                  placeholder="请输入请假天数"
                  controls-position="right"
                  :min="0"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <el-form-item label="审批意见" prop="message">
          <el-input v-model="form.message" type="textarea" placeholder="请输入审批意见"
                    :autosize="{minRows: 4, maxRows: 4}"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleBtn('PASS')">审批通过</el-button>
          <el-button @click="handleBtn('REJECT')">驳回</el-button>
          <el-button @click="cancel">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Approve">
import { getLeave, handle } from "@/api/test/leave";
const { proxy } = getCurrentInstance();
const { flow_status, leave_type } = proxy.useDict('flow_status', 'leave_type');

const props = defineProps({
  /* 业务id */
  modelValue: {
    type: String,
    default: "",
  },

  /* 实例id */
  taskId: {
    type: String,
    default: "",
  },

  /* 是否可以标编辑 */
  disabled: {
    type: Boolean,
    default: false,
  },
})
// 是否显示弹出层
const open = ref(false);
// 弹出层标题
const title = ref("");

const data = reactive({
  form: {},
  rules: {
    type: [
      { required: true, message: "请假类型不能为空", trigger: "change" }
    ],
    reason: [
      { required: true, message: "请假原因不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
    endTime: [
      { required: true, message: "结束时间不能为空", trigger: "blur" }
    ],
    day: [
      { required: true, message: "请假天数不能为空", trigger: "blur" }
    ],
  }
});

const { form, rules } = toRefs(data);
const emit = defineEmits(["refresh"]);

function getInfo() {
  reset();
  getLeave(props.modelValue).then(response => {
    if (!response.data) {
      proxy.$modal.alertWarning("代办任务不存在");
    }
    form.value = response.data;
  });
  title.value = "办理"
  open.value = true
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    message: null,
    id: null,
    tenantId: null,
    type: null,
    reason: null,
    startTime: null,
    endTime: null,
    day: null,
    instanceId: null,
    nodeCode: null,
    nodeName: null,
    flowStatus: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null
  };
  proxy.resetForm("leaveApproveRef");
}

/** 审核通过按钮 */
function handleBtn(skipType) {
  proxy.$refs["leaveApproveRef"].validate(valid => {
    if (valid) {
      handle(form.value, props.taskId, skipType, form.value.message).then(() => {
        proxy.$modal.msgSuccess("办理成功");
        open.value = false;
        emit('refresh');
      });
    }
  });
}

getInfo();
</script>
